package com.frame.pageform;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.frame.database.PageResult;
import com.frame.pageform.component.JsonObject;
import com.frame.pageform.component.JsonState;
import com.frame.util.Pagination;
import com.frame.util.SelectItem;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company:
 * @author 王海锋
 * @CreatedTime:2012-3-26 上午11:47:23
 * @version 1.0
 */

abstract public class QueryPageForm extends PageForm{

    private String[] primaryKey_;
    private int pageNum=1;
    private int pageSize_=20;
    protected Pagination pagination;
    private List resultList_;
    private String ok_;
    private boolean new_;

    private  Logger logger=Logger.getLogger(QueryPageForm.class);

    public List<SelectItem> getPageSizeList(){
        List<SelectItem> list=new ArrayList<SelectItem>();
        list.add(new SelectItem("20","20"));
        list.add(new SelectItem("30","30"));
        list.add(new SelectItem("50","50"));
        list.add(new SelectItem("100","100"));
        list.add(new SelectItem("200","200"));
        return list;
    }
    public String addShow() throws Exception{
        setNew_(true);
        return show();
    }
    public String updateShow() throws Exception{
        setNew_(false);
        return show();
    }
    public String del() throws Exception{
        return toDel();
    }
    abstract public String show() throws Exception;
    abstract public String toDel() throws Exception;
    /**
     * 保存数据具体实现
     * 功能:<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2012-3-26 下午02:40:07<br>
     * 说明:<br>
     * 变更:<br>
     */
    abstract public boolean toUpdate() throws Exception;
    /**
     * 保存数据后操作
     * 功能:<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2012-3-26 下午02:40:28<br>
     * 说明:<br>
     * 变更:<br>
     */
    abstract public void afterUpdate() throws Exception;

    public String update(){
        JsonObject json=getJsonObject();
        try{
            if(toUpdate()){
                json.getJsonPage().setListFlesh(true);
                afterUpdate();
                json.setStateCode(JsonState.OK);
                if(isNew_()){
                    json.getJsonPage().showOkMsg("添加成功!");
                    json.getJsonPage().setResetForm(true);
                }
                else{
                    json.getJsonPage().showOkMsg("保存成功!");
                    json.getJsonPage().closeWin();
                }
            }
        }catch(Exception e){
            json.setStateCode(JsonState.ERROR);
            if(isNew_())
                json.getJsonPage().showErrorMsg("添加失败!"+e.getMessage());
            else
                json.getJsonPage().showErrorMsg("保存失败!"+e.getMessage());
            logger.error("",e);
        }
        String contenType=RequestContext.getContext().getRequest().getContentType();
        if(contenType!=null && contenType.toLowerCase().indexOf("multipart/form-data")!=-1)
            return json.toPageString();
        else
            return json.toString();
    }
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageSize_() {
        return pageSize_;
    }

    public void setPageSize_(int pageSize_) {
        this.pageSize_ = pageSize_;
    }

    public String[] getPrimaryKey_() {
        return primaryKey_;
    }

    public void setPrimaryKey_(String[] primaryKey_) {
        this.primaryKey_ = primaryKey_;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }
    public void setPagination(PageResult pageRes) {
        this.pagination = new Pagination(pageRes.getPageSize(),pageRes.getPageNo(),pageRes.getPageCount(),pageRes.getList().size(),pageRes.getResultCount());;
        this.resultList_=pageRes.getList();
    }

    public List getResultList_() {
        return resultList_;
    }

    public void setResultList_(List resultList_) {
        this.resultList_ = resultList_;
    }
    public String getOk_() {
        return ok_;
    }

    public void setOk_(String ok_) {
        this.ok_ = ok_;
    }

    public boolean isNew_() {
        return new_;
    }

    public void setNew_(boolean new_) {
        this.new_ = new_;
    }
}
